using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The GuardianAddressTableDB class is responsible for interacting with the database to retrieve and store information
    /// about GuardianAddressTable objects.
    /// </summary>
    public class GuardianAddressTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of GuardianAddressTable from the underlying datasource.
        /// </summary>
        /// <param name="guardianAddressKey">The unique GuardianAddressKey of the GuardianAddressTable in the database.</param>
        /// <returns>An GuardianAddressTable when the GuardianAddressKey was found in the database, or null otherwise.</returns>
        public static GuardianAddressTable GetItem(int guardianAddressKey)
        {
            GuardianAddressTable myGuardianAddressTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianAddressTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@guardianAddressKey", guardianAddressKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myGuardianAddressTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myGuardianAddressTable;
            }
        }

        /// <summary>
        /// Gets an instance of GuardianAddressTable from the underlying datasource.
        /// </summary>
        /// <param name="UserAccountKey">The unique UserAccountKey of the GuardianAddressTable in the database.</param>
        /// <returns>An GuardianAddressTable when the GuardianAddressKey was found in the database, or null otherwise.</returns>
        public static GuardianAddressTable GetItemByUserAccountKey(int UserAccountKey, Int16 AddressTypeCode)
        {
            GuardianAddressTable myGuardianAddressTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianAddressTableSelectSingleItemByUserAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);
                    myCommand.Parameters.AddWithValue("@AddressTypeCode", AddressTypeCode);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myGuardianAddressTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myGuardianAddressTable;
            }
        }

        /// <summary>
        /// Returns a list with GuardianAddressTable objects.
        /// </summary>
        /// <returns>A generics List with the GuardianAddressTable objects.</returns>
        public static GuardianAddressTableList GetList()
        {
            GuardianAddressTableList tempList = new GuardianAddressTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianAddressTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Returns a list with GuardianAddressTable objects.
        /// </summary>
        /// <returns>A generics List with the GuardianAddressTable objects.</returns>
        public static GuardianAddressTableList GetListByUserAccountKey(int UserAccountKey)
        {
            GuardianAddressTableList tempList = new GuardianAddressTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianAddressTableSelectListByUserAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
                return tempList;
            }
        }

        /// <summary>
        /// Saves a GuardianAddressTable in the database.
        /// </summary>
        /// <param name="myGuardianAddressTable">The GuardianAddressTable instance to save.</param>
        /// <returns>The new GuardianAddressKey if the GuardianAddressTable is new in the database or the existing GuardianAddressKey when an item was updated.</returns>
        public static int Save(GuardianAddressTable myGuardianAddressTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianAddressTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myGuardianAddressTable.GuardianAddressKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@guardianAddressKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@guardianAddressKey", myGuardianAddressTable.GuardianAddressKey);
                    }
                    if (myGuardianAddressTable.UserAccountKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", myGuardianAddressTable.UserAccountKey);
                    }
                    if (string.IsNullOrEmpty(myGuardianAddressTable.UnitNo))
                    {
                        myCommand.Parameters.AddWithValue("@unitNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@unitNo", myGuardianAddressTable.UnitNo);
                    }
                    if (string.IsNullOrEmpty(myGuardianAddressTable.StreetName))
                    {
                        myCommand.Parameters.AddWithValue("@streetName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@streetName", myGuardianAddressTable.StreetName);
                    }
                    if (myGuardianAddressTable.CityCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", myGuardianAddressTable.CityCode);
                    }
                    if (myGuardianAddressTable.DistrictCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", myGuardianAddressTable.DistrictCode);
                    }
                    if (myGuardianAddressTable.TownshipCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@townshipCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@townshipCode", myGuardianAddressTable.TownshipCode);
                    }
                    if (myGuardianAddressTable.CountryCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", myGuardianAddressTable.CountryCode);
                    }
                    if (string.IsNullOrEmpty(myGuardianAddressTable.PostalCode))
                    {
                        myCommand.Parameters.AddWithValue("@postalCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@postalCode", myGuardianAddressTable.PostalCode);
                    }
                    if (myGuardianAddressTable.AddressTypeCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@addressTypeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@addressTypeCode", myGuardianAddressTable.AddressTypeCode);
                    }

                    myCommand.Parameters.AddWithValue("@sameAsStudentResidentAddress", myGuardianAddressTable.SameAsStudentResidentAddress);

                    if (myGuardianAddressTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myGuardianAddressTable.Status);
                    }
                    //if (myGuardianAddressTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myGuardianAddressTable.CreatedDateTime);
                    //}
                    //if (myGuardianAddressTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myGuardianAddressTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a GuardianAddressTable from the database.
        /// </summary>
        /// <param name="guardianAddressKey">The GuardianAddressKey of the GuardianAddressTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int guardianAddressKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianAddressTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@guardianAddressKey", guardianAddressKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the GuardianAddressTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static GuardianAddressTable FillDataRecord(IDataRecord myDataRecord)
        {
            GuardianAddressTable myGuardianAddressTable = new GuardianAddressTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("GuardianAddressKey")))
            {
                myGuardianAddressTable.GuardianAddressKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("GuardianAddressKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
            {
                myGuardianAddressTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UnitNo")))
            {
                myGuardianAddressTable.UnitNo = myDataRecord.GetString(myDataRecord.GetOrdinal("UnitNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StreetName")))
            {
                myGuardianAddressTable.StreetName = myDataRecord.GetString(myDataRecord.GetOrdinal("StreetName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
            {
                myGuardianAddressTable.CityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CityCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
            {
                myGuardianAddressTable.DistrictCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DistrictCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipCode")))
            {
                myGuardianAddressTable.TownshipCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TownshipCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
            {
                myGuardianAddressTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PostalCode")))
            {
                myGuardianAddressTable.PostalCode = myDataRecord.GetString(myDataRecord.GetOrdinal("PostalCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AddressTypeCode")))
            {
                myGuardianAddressTable.AddressTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("AddressTypeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SameAsStudentResidentAddress")))
            {
                myGuardianAddressTable.SameAsStudentResidentAddress = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("SameAsStudentResidentAddress"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myGuardianAddressTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myGuardianAddressTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myGuardianAddressTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myGuardianAddressTable;
        }
    }

}
